package com.zqh.reentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-13 21:39
 */

public class T2 {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Thread t1 =new Thread(()->{
           try{
               lock.lock();
               System.out.println("t1 start");
               TimeUnit.SECONDS.sleep(50000000);
               System.out.println("t1 end");
           }catch (InterruptedException e){
               e.printStackTrace();
           }finally {
               lock.unlock();
           }
        });

        t1.start();

        Thread t2 = new Thread(()->{

           try {
               lock.lockInterruptibly(); // 重点方法
               System.out.println("t2 start");
               TimeUnit.SECONDS.sleep(5);
               System.out.println("t2 end ");
           } catch (InterruptedException e) {
               System.out.println("interrupted!");
           }finally {
               lock.unlock();
           }
        });

        t2.start();

        try{
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        t2.interrupt();//打断t2的等待

    }
}
